package com.orm.mybatisplus.serviceImpl;


import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orm.mybatisplus.annotation.DataSource;
import com.orm.mybatisplus.config.DatabaseContextHolder;
import com.orm.mybatisplus.entity.AsusPoInfo;
import com.orm.mybatisplus.entity.User;
import com.orm.mybatisplus.mapper.master1.UserMapper1;
import com.orm.mybatisplus.mapper.master2.UserMapper2;
import com.orm.mybatisplus.mapper.master3.AsusPoInfoMapper3;
import com.orm.mybatisplus.util.DbUtil;
import org.springframework.aop.framework.AopContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper1,User> implements IService<User> {

    @Resource
    @Lazy
    private UserServiceImpl userService;

    @Resource
    private UserMapper1 userMapper1;

    @Resource
    private UserMapper2 userMapper2;

    @Resource
    private AsusPoInfoMapper3 asusPoInfoMapper3;

    public List<User> findAllUser(){
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER1);
        List<User> list = userMapper1.selectAllUser();
        DatabaseContextHolder.clearDBKey();
        return list;
    }

    public List<User> findAllUserForId6(){
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER1);
        List<User> list = userMapper1.selectAllUserForId6();
        DatabaseContextHolder.clearDBKey();
        return list;
    }

    public List<User> findAllUserForId8(){
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER2);
        List<User> list2 = userMapper2.selectAllUserForId8();
        DatabaseContextHolder.clearDBKey();
        return list2;
    }


    public void SystemOutPrintAll(){
        ((com.orm.mybatisplus.serviceImpl.UserServiceImpl) AopContext.currentProxy()).selectUserByIdMaster1(1L);
        ((com.orm.mybatisplus.serviceImpl.UserServiceImpl) AopContext.currentProxy()).selectAllUserMaster2();
        ((com.orm.mybatisplus.serviceImpl.UserServiceImpl) AopContext.currentProxy()).selectAllAsusPoInfo();
    }

    //    @Transactional   //加上切换失败
    public void SystemOutPrintAllNOAspect(){
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER1);
        System.out.println(userMapper1.selectUserByIdMaster1(1L));
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER2);
        System.out.println(userMapper2.selectAllUserMaster2());
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER3);
        System.out.println(asusPoInfoMapper3.selectAllAsusPoInfo());
    }

    @DataSource(value = DbUtil.DB_MASTER2)
    public List<User> selectAllUserMaster2(){
        List<User> list = userMapper2.selectAllUserMaster2();
        return list;
    }

    @DataSource
    public User selectUserByIdMaster1(Long id){
        return userMapper1.selectUserByIdMaster1(id);
    }

    @DataSource(value = DbUtil.DB_MASTER3)
    public AsusPoInfo selectAllAsusPoInfo(){
        return asusPoInfoMapper3.selectAllAsusPoInfo();
    }

    public int insertUser(User user){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(simpleDateFormat.format(new Date()));
        user.setLastLoginTime(simpleDateFormat.format(new Date()));
        user.setLastUpdateTime(simpleDateFormat.format(new Date()));
        return userMapper1.saveUser(user);
    }



    public void testTransitional(){
        ((UserServiceImpl)AopContext.currentProxy()).saveUserMapper2();
        ((UserServiceImpl)AopContext.currentProxy()).saveUserMapper1();
        ((UserServiceImpl)AopContext.currentProxy()).saveUserMapper3();
    }

    @Transactional
    public void testTransitionalNoAspect() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        Random random = new Random(500);
        int a = random.nextInt();
        User user = User.builder().email("andrew@qq.com"+a).name("andrew"+a).password("123456").phoneNumber("123"+a)
                .lastUpdateTime(date).createTime(date).status(0).salt("password").build();
        AsusPoInfo asusPoInfo = AsusPoInfo.builder().id(UUID.randomUUID().toString().substring(0,20))
                .woNo("andrew").po("123456").poLine("poline").cPo("cpo123456").shipType("Direct").build();
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER1);
        userMapper1.saveUser(user);
        DatabaseContextHolder.clearDBKey();
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER2);
        userMapper2.saveUser(user);
        DatabaseContextHolder.clearDBKey();
        DatabaseContextHolder.setDBKey(DbUtil.DB_MASTER3);
        asusPoInfoMapper3.insertAsusPoInfo(asusPoInfo);
        DatabaseContextHolder.clearDBKey();
        throw new RuntimeException();
    }



    @Transactional
    @DataSource(value = DbUtil.DB_MASTER1)
    public void saveUserMapper1(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        String UUID = java.util.UUID.randomUUID().toString().substring(0,5);
        User user = User.builder().email("andrew@qq.com"+UUID).name("andrew"+UUID).password("123456"+UUID).phoneNumber("123"+UUID)
                .lastUpdateTime(date).createTime(date).status(0).salt("password"+UUID).build();
//        userMapper2.saveUser(user);
        userService.save(user);
    }

    @Transactional
    @DataSource(value = DbUtil.DB_MASTER2)
    public void saveUserMapper2(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        String UUID = java.util.UUID.randomUUID().toString().substring(0,5);
        User user = User.builder().email("andrew@qq.com"+UUID).name("andrew"+UUID).password("123456"+UUID).phoneNumber("123"+UUID)
                .lastUpdateTime(date).createTime(date).status(0).salt("password"+UUID).build();
        userMapper2.saveUser(user);

    }

    @DataSource(value = DbUtil.DB_MASTER3)
    @Transactional
    public void saveUserMapper3(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        Random random = new Random(500);
        int a = random.nextInt();
        AsusPoInfo asusPoInfo = AsusPoInfo.builder().id(UUID.randomUUID().toString().substring(0,20))
                .woNo("andrew").po("123456").poLine("poline").cPo("cpo123456").shipType("Direct").build();
        asusPoInfoMapper3.insertAsusPoInfo(asusPoInfo);
    }



}
